﻿namespace Probel.Statistics
{
    using System;
    using System.Linq;

    using Probel.Statistics.Domain;
    using Probel.Statistics.Helpers;

    public class StatReader : IStatReader
    {

        #region Methods

        public ApplicationsSummary GetApplicationsSummary()
        {
            return this.GetApplicationsSummaryBefore(DateTime.MaxValue);
        }

        public ApplicationsSummary GetApplicationsSummaryBefore(DateTime threshold)
        {
            using (var db = new DataContext())
            {
                var result = new ApplicationsSummary();

                result.LdocCount = (from a in db.Applications
                                    where a.AppName.ToLower() == AppNames.LDoc
                                       && a.LastConnection <= threshold
                                    select a).Count();

                result.NDoctorCount = (from a in db.Applications
                                       where a.AppName.ToLower() == AppNames.NDoctor
                                          && a.LastConnection <= threshold
                                       select a).Count();

                result.OtherCount = (from a in db.Applications
                                     where a.AppName.ToLower() != AppNames.NDoctor
                                        && a.AppName.ToLower() != AppNames.LDoc
                                        && a.LastConnection <= threshold
                                     select a).Count();

                var app = (from a in db.Applications
                           where a.LastConnection <= threshold
                           select a).ToArray();

                result.WindowsVersions = (from a in app
                                          group a by a.WindowsVersion into g
                                          select new WindowsVersion()
                                          {
                                              Name = g.Key,
                                              Count = g.Count(),
                                          }).ToList();
                result.WindowsNames = (from a in app
                                       group a by a.WindowsVersion into g
                                       select new WindowsVersion()
                                       {
                                           Name = g.Key,
                                           Count = g.Count(),
                                       }).ToList();

                return result;
            }
        }

        public Listing GetLogEntries()
        {
            using (var db = new DataContext())
            {
                var result = (from a in db.Applications
                              select new LogEntry()
                              {
                                  LastConnection = a.LastConnection,
                                  RegistrationDate = a.RegistrationDate,
                                  WindowsName = a.WindowsVersion
                              }).ToList();
                return new Listing(result);
            }
        }

        #endregion Methods
    }
}